package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.entity.ManualRecord;
import com.qcby.mycommunity_003.mapper.ManualRecordMapper;
import com.qcby.mycommunity_003.service.CommunityService;
import com.qcby.mycommunity_003.service.ManualRecordService;
import com.qcby.mycommunity_003.vo.ManualListVo;
import com.qcby.mycommunity_003.vo.ManualResultVo;
import com.qcby.mycommunity_003.vo.PageVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-28
 */
@Service
public class ManualRecordServiceImpl extends ServiceImpl<ManualRecordMapper, ManualRecord> implements ManualRecordService {

    @Autowired
    private ManualRecordMapper manualRecordMapper;
    @Autowired
    private CommunityService communityService;

    @Override
    public PageVo ManualPage(ManualListVo manualListVo) {
        //对mybatis内置的分页对象封装
        Page<ManualRecord> page=new Page<>();
        page.setSize(manualListVo.getLimit());
        page.setCurrent(manualListVo.getPage());
        //封装查询的条件
        QueryWrapper<ManualRecord> queryWrapper=new QueryWrapper<>();
       queryWrapper.eq(StringUtils.isNotBlank(manualListVo.getMobile()),"mobile",manualListVo.getMobile());
       queryWrapper.eq(manualListVo.getCommunityId()!=null,"community_id",manualListVo.getCommunityId());
       queryWrapper.eq(StringUtils.isNotBlank(manualListVo.getVisitor()),"visitor",manualListVo.getVisitor());
        Page<ManualRecord> page1 = manualRecordMapper.selectPage(page, queryWrapper);
        ArrayList<ManualResultVo> list=new ArrayList<>();
        for (ManualRecord record : page.getRecords()) {
            //创建变量
            ManualResultVo manualResultVo=new ManualResultVo();
            BeanUtils.copyProperties(record,manualResultVo);
            manualResultVo.setCommunityName(communityService.getById(record.getCommunityId()).getCommunityName());
            list.add(manualResultVo);
        }
        PageVo pageVo=new PageVo();
        pageVo.setList(list);
        pageVo.setTotalPage(page1.getPages());
        pageVo.setPageSize(page1.getSize());
        pageVo.setCurrPage(page1.getCurrent());
        pageVo.setTotalCount(page1.getTotal());
        return pageVo;
    }
}
